package com.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.controller.vo.CommisVo;
import com.controller.vo.CustomerVo;
import com.dao.ICommissionerDao;
import com.dao.ICustomerDao;
import com.dao.IRCustCommisDao;
import com.dao.entity.RCustCommis;
import com.service.IRCustCommisService;
import com.utils.commons.PageUtils;
import com.utils.enums.ENUM_SUCESS;

@Service
public class RCustCommisServiceImpl implements IRCustCommisService{
	
	@Autowired
	private IRCustCommisDao rCustCommisDao;
	
	@Autowired
	private ICustomerDao customerDao;
	
	@Autowired
	private ICommissionerDao commissionerDao;

	@Override
	public Page<CustomerVo> getMyCustomers(Long commisId, Integer page, Integer size) {
		if(ObjectUtils.isEmpty(page)) {
			page=0;
		}
		if(ObjectUtils.isEmpty(size)) {
			size=10;
		}
		Pageable pageable = PageUtils.toPageable(page, size);
		Page<RCustCommis> pageResult = rCustCommisDao.findByCommisId(commisId, pageable);
		List<CustomerVo> temp = new ArrayList<CustomerVo>();
		pageResult.forEach(e ->{
			temp.add(new CustomerVo(customerDao.findById(e.getCustomerId()).get()));
		});
		Page<CustomerVo> result = new PageImpl<CustomerVo>(temp, pageable, pageResult.getTotalElements());
		return result;
	}
	
	@Override
	public Page<CommisVo> getMyCommis(Long customerId, Integer page, Integer size) {
		if(ObjectUtils.isEmpty(page)) {
			page=0;
		}
		if(ObjectUtils.isEmpty(size)) {
			size=10;
		}
		Pageable pageable = PageUtils.toPageable(page, size);
		Page<RCustCommis> pageResult = rCustCommisDao.findByCustomerId(customerId, pageable);
		List<CommisVo> temp = new ArrayList<CommisVo>();
		pageResult.forEach(e ->{
			temp.add(new CommisVo(commissionerDao.findByIdAndIsApproval(e.getCommisId(), ENUM_SUCESS.YES.value)));
		});
		Page<CommisVo> result = new PageImpl<CommisVo>(temp, pageable, pageResult.getTotalElements());
		return result;
	}

}
